{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from glob import glob\n",
    "from pathlib import Path\n",
    "import json \n",
    "from itertools import chain\n",
    "import os\n",
    "from tqdm.auto import tqdm\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c6f0215a6cef4193abac16d99579de00",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "caoncat:   0%|          | 0/171 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "value: 829039\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "829039"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def extract_filename_with_parent(full_path):\n",
    "    # 使用os.path.basename获取文件名（包括扩展名）\n",
    "    filename_with_ext = os.path.basename(full_path)\n",
    "\n",
    "    # 使用os.path.dirname获取父文件夹名称\n",
    "    parent_folder = os.path.basename(os.path.dirname(full_path))\n",
    "\n",
    "    # 使用os.path.splitext分离文件名和扩展名\n",
    "    filename_without_ext, _ = os.path.splitext(filename_with_ext)\n",
    "\n",
    "    # 组合父文件夹名和文件名（不包括扩展名）\n",
    "    result = f\"{parent_folder}/{filename_without_ext}\"\n",
    "\n",
    "    return result\n",
    "\n",
    "\n",
    "def concat_all_trans_data():\n",
    "\n",
    "    count_value = 0\n",
    "    all_file_name = glob(\"data/temp_trans_data/*.json\")\n",
    "\n",
    "    def load_file_data(file_name):\n",
    "        with open(file_name, \"r\") as fin:\n",
    "            alldata = fin.readlines()\n",
    "\n",
    "        alldata = [json.loads(i) for i in alldata]\n",
    "        res = []\n",
    "\n",
    "        for i in alldata:\n",
    "            i[\"file_path\"] = extract_filename_with_parent(i[\"file_path\"])\n",
    "            res.append(i)\n",
    "        return res\n",
    "\n",
    "\n",
    "\n",
    "    with open(\"data/jackyhate/text-to-image-2M/big_tran_zh_data.json\", mode='w', encoding='utf-8') as fout:\n",
    "        for file_name in tqdm(all_file_name, desc='caoncat'):\n",
    "            alldata = load_file_data(file_name)\n",
    "            for i in alldata:\n",
    "                count_value+=1\n",
    "                fout.write(json.dumps(i, ensure_ascii=False) + '\\n')\n",
    "\n",
    "    \n",
    "    print(f\"value: {count_value}\")\n",
    "    # alldata = [load_file_data(i) for i in tqdm(all_file_name, desc='caoncat')]\n",
    "    # alldata = list(chain(*alldata))\n",
    "\n",
    "    return count_value\n",
    "concat_all_trans_data()\n",
    "\n",
    "# alldata = concat_all_trans_data()\n",
    "# len(alldata), alldata[:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "data_000030/llava_next_captioned_700K_00042490\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "\n",
    "# 测试函数\n",
    "test_path = (\n",
    "    \"data/jackyhate/unzip2mdata/data_000030/llava_next_captioned_700K_00042490.json\"\n",
    ")\n",
    "print(extract_filename_with_parent(test_path))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
